package handler

import (
	"article/api/request"
	"article/inter/dao"
	"article/inter/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CreateArt(c *gin.Context) {
	var req request.CreateArt
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var article model.Articles
	dao.GetOneByFields(&model.Articles{
		Title: req.Title,
	}, &article)
	if article.Id != 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "此篇文章已存在",
			"data": nil,
		})
		return
	}
	article = model.Articles{
		Title:  req.Title,
		Common: req.Common,
		Like:   uint32(req.Like),
		Look:   uint32(req.Look),
	}
	if !dao.Create(&article) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "文章添加失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "文章添加成功",
		"data": nil,
	})
	return
}
